package com.zhaoqi.hr.repository;

import com.zhaoqi.hr.entity.JobPosting;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 岗位发布数据访问层
 */
@Repository
public interface JobPostingRepository extends JpaRepository<JobPosting, Long>, JpaSpecificationExecutor<JobPosting> {

    /**
     * 根据雇主ID查询岗位列表，按创建时间倒序
     */
    List<JobPosting> findByEmployer_User_IdOrderByCreatedAtDesc(Long employerId);

    /**
     * 根据状态查询岗位列表，按创建时间倒序（分页）
     */
    Page<JobPosting> findByStatusOrderByCreatedAtDesc(JobPosting.JobStatus status, Pageable pageable);

    /**
     * 查询所有岗位，按创建时间倒序（分页）
     */
    Page<JobPosting> findAllByOrderByCreatedAtDesc(Pageable pageable);

    /**
     * 根据状态、岗位类型和地点查询岗位（分页）
     */
    Page<JobPosting> findByStatusAndJobTypeAndLocationContaining(
            JobPosting.JobStatus status, 
            JobPosting.JobType jobType, 
            String location, 
            Pageable pageable);

    /**
     * 查询活跃岗位
     */
    @Query("SELECT jp FROM JobPosting jp WHERE jp.status = 'ACTIVE' ORDER BY jp.createdAt DESC")
    List<JobPosting> findActiveJobPostings();

    /**
     * 根据技能标签搜索岗位
     */
    @Query("SELECT jp FROM JobPosting jp WHERE jp.skillRequirements LIKE %:skill% AND jp.status = 'ACTIVE'")
    List<JobPosting> findBySkillRequirementsContaining(@Param("skill") String skill);

    /**
     * 根据地点搜索岗位
     */
    List<JobPosting> findByLocationContainingAndStatus(String location, JobPosting.JobStatus status);

    /**
     * 根据岗位类型查询岗位
     */
    List<JobPosting> findByJobTypeAndStatus(JobPosting.JobType jobType, JobPosting.JobStatus status);

    /**
     * 统计雇主发布的岗位数量
     */
    long countByEmployer_User_Id(Long employerId);

    /**
     * 统计各状态岗位数量
     */
    long countByStatus(JobPosting.JobStatus status);

    /**
     * 根据状态统计岗位数量（字符串参数）
     */
    long countByStatus(String status);
}
